﻿#include "ToolBarFilter.h"

#include "OperatorsInterface/GraphEventOperator.h"
#include "PostGraphAdaptor/PostGraphObjectBase.h"
#include "PostGraphAdaptor/PostGraphProperty.h"
#include "PostGraphAdaptor/PostGraphObjectManager.h"

#include "FITK_Interface/FITKInterfaceCFDPost/FITKAbstractCFDPostData.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPost3DManager.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "GUIFrame/MainWindow.h"

namespace GUI {
    ToolBarFilter::ToolBarFilter(SARibbonPannel* pannel) :
        ToolBarBase(pannel) {
        _isoSurfAction = createAction(tr("isosurf"), "actionIsosurfCreate", "://icons/isosurf.png", tr("isosurf"));
        this->addAction(_isoSurfAction);
        _vectorfAction = createAction(tr("vector"), "actionGlyphCreate", "://icons/vector.png", tr("vector"));
        this->addAction(_vectorfAction);
        _clipAction = createAction(tr("clip"), "actionClipCreate", "://icons/clip.png", tr("clip"));
        this->addAction(_clipAction);
        _sliceAction = createAction(tr("slice"), "actionSliceCreate", "://icons/slice.png", tr("slice"));
        this->addAction(_sliceAction);
        _streamlineSurfAction = createAction(tr("streamline"), "actionStreamlineCreate", "://icons/streamline.png", tr("streamline"));
        this->addAction(_streamlineSurfAction);
        _thresholdAction = createAction(tr("threshold"),"actionThresholdCreate", "://icons/threshold.png", tr("threshold"));
        this->addAction(_thresholdAction);
        _deformAction = createAction(tr("deform"), "actionDeformCreate", ":FITKIcons/icoR_deform.svg",tr("deform"));
        this->addAction(_deformAction);
        QAction* action = createAction(tr("probe"), "actionProbeCreate", "://icons/probe.png", tr("probe"));
        this->addAction(action);

        updateCurrentGraphObj(-1);
    }

    ToolBarFilter::~ToolBarFilter() {

    }

    void ToolBarFilter::updateCurrentGraphObj(int objID) {
        _currentDataID = objID;

        updateIsoSurf();
        updateVector();
        updateClip();
        updateSlice();
        updateStreamLine();
        updateThreshold();
        updateDeform();
    }

    void ToolBarFilter::updateIsoSurf() {
        _isoSurfAction->setEnabled(false);
        Interface::FITKAbstractCFDPostData* currentData = _postDataManager->getDataByID(_currentDataID);
        if (currentData == nullptr) {
            return;
        }
        //判断当前数据是否可见
        if (currentData->isEnable() == false) {
            return;
        }

        QStringList pointList = currentData->getPointDataArray();
        QStringList contourList = {};
        //点标量数据
        for (QString point : pointList) {
            Interface::FITKPostVariableType type = currentData->getVariableType(Interface::FITKPostFieldType::Post_Point, point);
            if (type == Interface::FITKPostVariableType::Post_Scalar) {
                contourList.append(point);
            }
        }

        //判断当前数据是否有可用标量
        if (contourList.isEmpty() == false) {
            _isoSurfAction->setEnabled(true);
        }
    }

    void ToolBarFilter::updateVector() {
        _vectorfAction->setEnabled(false);
        Interface::FITKAbstractCFDPostData* currentData = _postDataManager->getDataByID(_currentDataID);
        if (currentData == nullptr) {
            return;
        }
        //判断当前数据是否可见
        if (currentData->isEnable() == false) {
            return;
        }
        _vectorfAction->setEnabled(true);
    }

    void ToolBarFilter::updateClip() {
        _clipAction->setEnabled(false);
        Interface::FITKAbstractCFDPostData* currentData = _postDataManager->getDataByID(_currentDataID);
        if (currentData == nullptr) {
            return;
        }
        //判断当前数据是否可见
        if (currentData->isEnable() == false) {
            return;
        }
        _clipAction->setEnabled(true);
    }

    void ToolBarFilter::updateSlice() {
        _sliceAction->setEnabled(false);
        Interface::FITKAbstractCFDPostData* currentData = _postDataManager->getDataByID(_currentDataID);
        if (currentData == nullptr) {
            return;
        }
        //判断当前数据是否可见
        if (currentData->isEnable() == false) {
            return;
        }
        _sliceAction->setEnabled(true);
    }

    void ToolBarFilter::updateStreamLine() {
        _streamlineSurfAction->setEnabled(false);
        Interface::FITKAbstractCFDPostData* currentData = _postDataManager->getDataByID(_currentDataID);
        if (currentData == nullptr) {
            return;
        }
        //判断当前数据是否可见
        if (currentData->isEnable() == false) {
            return;
        }

        //获取矢量
        QStringList vectorList = {};
        QStringList pointDataArray = currentData->getPointDataArray();
        for (QString point : pointDataArray) {
            Interface::FITKPostVariableType type = currentData->getVariableType(Interface::FITKPostFieldType::Post_Point, point);
            if (type == Interface::FITKPostVariableType::Post_Vector) {
                vectorList.append(point);
            }
        }
        QStringList cellDataArray = currentData->getCellDataArray();
        for (QString cell : cellDataArray) {
            Interface::FITKPostVariableType type = currentData->getVariableType(Interface::FITKPostFieldType::Post_Cell, cell);
            if (type == Interface::FITKPostVariableType::Post_Vector) {
                vectorList.append(cell);
            }
        }

        //判断当前数据是否有可用矢量
        if (vectorList.isEmpty() == false) {
            _streamlineSurfAction->setEnabled(true);
        }
    }

    void ToolBarFilter::updateThreshold() {
        _thresholdAction->setEnabled(false);
        Interface::FITKAbstractCFDPostData* currentData = _postDataManager->getDataByID(_currentDataID);
        if (currentData == nullptr) {
            return;
        }
        //判断当前数据是否可见
        if (currentData->isEnable() == false) {
            return;
        }
        _thresholdAction->setEnabled(true);
    }

    void ToolBarFilter::updateDeform() {
        _deformAction->setEnabled(false);
        Interface::FITKAbstractCFDPostData* currentData = _postDataManager->getDataByID(_currentDataID);
        if (currentData == nullptr) {
            return;
        }
        //判断当前数据是否可见
        if (currentData->isEnable() == false) {
            return;
        }

        //添加变量
        int vectorNum = 0;
        QStringList pointDataArray = currentData->getPointDataArray();
        for (QString pointData : pointDataArray) {
            Interface::FITKPostVariableType type = currentData->getVariableType(Interface::FITKPostFieldType::Post_Point, pointData);
            if (type == Interface::FITKPostVariableType::Post_Error || type == Interface::FITKPostVariableType::Post_Scalar) {
                continue;
            }
            vectorNum++;
        }
        if (vectorNum <= 0) {
            return;
        }
        _deformAction->setEnabled(true);
    }
}
